﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DeviceInfoManage.DataAccessLayer;

namespace DeviceInfoManage.BusinessLogicLayer
{
    [Serializable]
    public class LeaderInspect
    {
        #region 字段属性
        private int _id;
        private string _username;
        private string _roomnumber;
        private string _item;
        private string _principal;
        private DateTime _date;
        private string _detail;
        private string _suggestion;
        /// <summary>
        /// ID
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 使用人
        /// </summary>
        public string UserName
        {
            set { _username = value; }
            get { return _username; }
        }
        /// <summary>
        /// 办公室
        /// </summary>
        public string RoomNumber
        {
            set { _roomnumber = value; }
            get { return _roomnumber; }
        }
        /// <summary>
        /// 巡检项目
        /// </summary>
        public string Item
        {
            set { _item = value; }
            get { return _item; }
        }
        /// <summary>
        /// 巡检负责人
        /// </summary>
        public string Principal
        {
            set { _principal = value; }
            get { return _principal; }
        }
        /// <summary>
        /// 巡检日期
        /// </summary>
        public DateTime Date
        {
            set { _date = value; }
            get { return _date; }
        }
        /// <summary>
        /// 巡检详细情况
        /// </summary>
        public string Detail
        {
            set { _detail = value; }
            get { return _detail; }
        }
        /// <summary>
        /// 反馈信息/使用建议
        /// </summary>
        public string Suggestion
        {
            set { _suggestion = value; }
            get { return _suggestion; }
        }
        #endregion

        #region 构造函数
        public LeaderInspect()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        #endregion

        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "LeaderInspect");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from LeaderInspect");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(LeaderInspect model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into LeaderInspect(");
            strSql.Append("UserName,RoomNumber,Item,Principal,Date,Detail,Suggestion)");
            strSql.Append(" values (");
            strSql.Append("@UserName,@RoomNumber,@Item,@Principal,@Date,@Detail,@Suggestion)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@RoomNumber", SqlDbType.VarChar,50),
					new SqlParameter("@Item", SqlDbType.VarChar,50),
					new SqlParameter("@Principal", SqlDbType.VarChar,50),
					new SqlParameter("@Date", SqlDbType.DateTime),
					new SqlParameter("@Detail", SqlDbType.VarChar,1000),
					new SqlParameter("@Suggestion", SqlDbType.VarChar,1000)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.RoomNumber;
            parameters[2].Value = model.Item;
            parameters[3].Value = model.Principal;
            parameters[4].Value = model.Date;
            parameters[5].Value = model.Detail;
            parameters[6].Value = model.Suggestion;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(LeaderInspect model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update LeaderInspect set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("RoomNumber=@RoomNumber,");
            strSql.Append("Item=@Item,");
            strSql.Append("Principal=@Principal,");
            strSql.Append("Date=@Date,");
            strSql.Append("Detail=@Detail,");
            strSql.Append("Suggestion=@Suggestion");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@RoomNumber", SqlDbType.VarChar,50),
					new SqlParameter("@Item", SqlDbType.VarChar,50),
					new SqlParameter("@Principal", SqlDbType.VarChar,50),
					new SqlParameter("@Date", SqlDbType.DateTime),
					new SqlParameter("@Detail", SqlDbType.VarChar,1000),
					new SqlParameter("@Suggestion", SqlDbType.VarChar,1000)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.UserName;
            parameters[2].Value = model.RoomNumber;
            parameters[3].Value = model.Item;
            parameters[4].Value = model.Principal;
            parameters[5].Value = model.Date;
            parameters[6].Value = model.Detail;
            parameters[7].Value = model.Suggestion;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from LeaderInspect ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public LeaderInspect GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,UserName,RoomNumber,Item,Principal,Date,Detail,Suggestion from LeaderInspect ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            LeaderInspect model = new LeaderInspect();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                model.RoomNumber = ds.Tables[0].Rows[0]["RoomNumber"].ToString();
                model.Item = ds.Tables[0].Rows[0]["Item"].ToString();
                model.Principal = ds.Tables[0].Rows[0]["Principal"].ToString();
                if (ds.Tables[0].Rows[0]["Date"].ToString() != "")
                {
                    model.Date = DateTime.Parse(ds.Tables[0].Rows[0]["Date"].ToString());
                }
                model.Detail = ds.Tables[0].Rows[0]["Detail"].ToString();
                model.Suggestion = ds.Tables[0].Rows[0]["Suggestion"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserName,RoomNumber,Item,Principal,Date,Detail,Suggestion ");
            strSql.Append(" FROM LeaderInspectView ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,UserName,RoomNumber,Item,Principal,Date,Detail,Suggestion ");
            strSql.Append(" FROM LeaderInspect ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  成员方法

    }
}
